﻿using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net;
using System.Web.Services;

/// <summary>
/// Summary description for facebook_count_service
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class facebook_count_service : System.Web.Services.WebService {

    public facebook_count_service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public UrlDetails[] BindDatatable(string urltxt)
    {
        List<UrlDetails> details = new List<UrlDetails>();
        WebClient web = new WebClient();
        string url = string.Format("https://api.facebook.com/method/fql.query?query=SELECT url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" + urltxt + "'");
        string response = web.DownloadString(url);
        DataSet ds = new DataSet();
        using (StringReader stringReader = new StringReader(response))
        {
            ds = new DataSet();
            ds.ReadXml(stringReader);
        }
        DataTable dt = ds.Tables["link_stat"];
        foreach (DataRow dtrow in dt.Rows)
        {
            UrlDetails website = new UrlDetails();
            website.Url = dtrow["url"].ToString();
            website.LikeCount = dtrow["like_count"].ToString();
            website.SharedCount = dtrow["share_count"].ToString();
            website.CommentCount = dtrow["comment_count"].ToString();
            website.ClickCount = dtrow["click_count"].ToString();
            website.TotalCount = dtrow["total_count"].ToString();
            details.Add(website);
        }
        return details.ToArray();
    }
    public class UrlDetails
    {
        public string Url { get; set; }
        public string SharedCount { get; set; }
        public string LikeCount { get; set; }
        public string CommentCount { get; set; }
        public string ClickCount { get; set; }
        public string TotalCount { get; set; }
    }
    
}
